{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 广播机制\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61a42cc1-9923-4983-973d-b03b59d6acc1",
   "metadata": {},
   "source": [
    "这段代码展示了 NumPy 的广播机制，通过定义矩阵、标量、列向量和行向量进行一系列加法操作。代码首先定义了一个 $3 \\times 2$ 的矩阵 $A$、一个标量 $k$、一个列向量 $c$ 和一个行向量 $r$。它们的具体定义如下：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \\end{bmatrix}, \\quad k = 2, \\quad c = \\begin{bmatrix} 3 \\\\ 2 \\\\ 1 \\end{bmatrix}, \\quad r = \\begin{bmatrix} 2 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码使用 NumPy 广播机制，分别计算以下几种加法操作：\n",
    "\n",
    "1. **矩阵 $A$ 与标量 $k$ 相加**：标量 $k$ 会广播到矩阵 $A$ 的每个元素，即每个元素都加上 $k$ 值。这种情况下，运算结果为：\n",
    "\n",
    "   $$\n",
    "   A + k = \\begin{bmatrix} 1+2 & 2+2 \\\\ 3+2 & 4+2 \\\\ 5+2 & 6+2 \\end{bmatrix} = \\begin{bmatrix} 3 & 4 \\\\ 5 & 6 \\\\ 7 & 8 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **矩阵 $A$ 与列向量 $c$ 相加**：列向量 $c$ 被广播到矩阵 $A$ 的每一列，使得 $c$ 的每个元素与 $A$ 相同行的每个元素相加，得到：\n",
    "\n",
    "   $$\n",
    "   A + c = \\begin{bmatrix} 1+3 & 2+3 \\\\ 3+2 & 4+2 \\\\ 5+1 & 6+1 \\end{bmatrix} = \\begin{bmatrix} 4 & 5 \\\\ 5 & 6 \\\\ 6 & 7 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "3. **矩阵 $A$ 与行向量 $r$ 相加**：行向量 $r$ 被广播到矩阵 $A$ 的每一行，使得 $r$ 的每个元素与 $A$ 相同行的每个元素相加，结果为：\n",
    "\n",
    "   $$\n",
    "   A + r = \\begin{bmatrix} 1+2 & 2+1 \\\\ 3+2 & 4+1 \\\\ 5+2 & 6+1 \\end{bmatrix} = \\begin{bmatrix} 3 & 3 \\\\ 5 & 5 \\\\ 7 & 7 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "4. **列向量 $c$ 与行向量 $r$ 相加**：通过广播，$c$ 和 $r$ 组合成一个 $3 \\times 2$ 的矩阵。每个 $c$ 的元素加到每一行的 $r$ 上，得到：\n",
    "\n",
    "   $$\n",
    "   c + r = \\begin{bmatrix} 3+2 & 3+1 \\\\ 2+2 & 2+1 \\\\ 1+2 & 1+1 \\end{bmatrix} = \\begin{bmatrix} 5 & 4 \\\\ 4 & 3 \\\\ 3 & 2 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "该代码展示了 NumPy 广播机制的灵活性，使得矩阵、向量和标量间的加法运算在不同维度下可以直接进行。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbbb9e8f-480a-4b84-be1c-10299faa79c3",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2da2788b-62a1-4402-bcc8-7c4448392cb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库，用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "371997be-719d-416d-9e23-6c0c9ae9bc96",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7acefee2-ddb0-4ec5-924e-b15c468f3bda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4],\n",
       "        [5, 6]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.matrix([[1, 2],  # 定义矩阵A\n",
    "               [3, 4],\n",
    "               [5, 6]])\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a695575-9a76-41e6-bb17-6d77d1dbca49",
   "metadata": {},
   "source": [
    "## 定义标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7b44157d-b5c4-450c-b5a9-8c17ec1fb9fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 2  # 定义标量k"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "950972dd-22ff-4a81-9793-2172ba4cc752",
   "metadata": {},
   "source": [
    "## 定义列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f59ea6bd-d607-48cf-8a7f-80eb5aac438c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [2],\n",
       "       [1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[3],  # 定义列向量c\n",
    "              [2],\n",
    "              [1]])\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a4a65e0-ce82-414a-990b-64713bca3913",
   "metadata": {},
   "source": [
    "## 定义行向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c571bb39-d6b7-44ea-b5d7-2b197223eb5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 1]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = np.array([[2, 1]])  # 定义行向量r\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a7d2094-3824-4efa-a099-40781551d1ff",
   "metadata": {},
   "source": [
    "## 广播原则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82e777a0-142f-4a7d-b3e1-2e38af1fe399",
   "metadata": {},
   "source": [
    "## 矩阵A加标量k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c7c158d0-9576-471c-953f-8767f19d9473",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3, 4],\n",
       "        [5, 6],\n",
       "        [7, 8]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_plus_k = A + k  # 矩阵A与标量k相加\n",
    "A_plus_k"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07c8aa0c-d857-4267-a1b7-99c7fc856f6d",
   "metadata": {},
   "source": [
    "## 矩阵A加列向量c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "919ef4b1-d43d-4c1e-933c-f9c8fde768f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[4, 5],\n",
       "        [5, 6],\n",
       "        [6, 7]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_plus_c = A + c  # 矩阵A与列向量c相加\n",
    "A_plus_c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07bd091a-0642-400a-8fd2-57ea4588875a",
   "metadata": {},
   "source": [
    "## 矩阵A加行向量r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a18f414-460e-452e-808e-47fd9d339345",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3, 3],\n",
       "        [5, 5],\n",
       "        [7, 7]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_plus_r = A + r  # 矩阵A与行向量r相加\n",
    "A_plus_r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fd72975-156a-46c3-abce-170467f5d416",
   "metadata": {},
   "source": [
    "## 列向量c加行向量r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e5ad12d3-b203-4a4e-90c4-20b693dfa4a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 4],\n",
       "       [4, 3],\n",
       "       [3, 2]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_plus_r = c + r  # 列向量c与行向量r相加\n",
    "c_plus_r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
